/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package rejeev.tracer;

//import Tracer;
import java.lang.instrument.Instrumentation;
import java.lang.management.ManagementFactory;
import javax.management.MBeanServer;
import javax.management.ObjectName;

/**
 *
 * @author rejeev.divakaran
 */
public class BCInstrumentation {
    private static Instrumentation instrumentation;
    public static void premain(String args, Instrumentation inst){
        try {
            System.out.println("Beginning of Method-Tracer");
            instrumentation = inst;
            registerMBean();
            EntryExitTransformer transformer = new EntryExitTransformer();
            /*
            Properties props = parseArgs(args);
            //setting allThreadsMonitoring flag
            // default is false
            String allThreads = props.getProperty("allThreads");
            if(allThreads != null && allThreads.equals("true")){
                Tracer.setAllThreads(true);
            }
            //setting Thread filters
            String threads = props.getProperty("threads");
            if(threads != null){
                Set<String> threadsToInclude = parseArgument(threads);
                for(String t : threadsToInclude){
                    Tracer.addThread(t);
                }
            }
            //setting log handlers
            String logHandlersString = props.getProperty("logs");
            if(logHandlersString != null){
                Tracer.setLogHandlers(parseArgument(logHandlersString));
            }
            //setting include package filters
            String includePackages = props.getProperty("includePackages");
            if(includePackages != null){
                transformer.setIncludeList(parseArgument(includePackages));
            }
            //setting exclude package filters
            String excludePackages = props.getProperty("excludePackages");
            if(excludePackages != null){
                transformer.setExcludeList(parseArgument(excludePackages));
            }
            */
            instrumentation.addTransformer(transformer);
            System.out.println("premain is completed");
            //Tracer.entry("dfdfd", "fdfdfdf");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        //System.out.println("premain is called");
        //Tracer.entry("dfdfd", "fdfdfdf");
    }
    /*
    //reading the properties from the javaagent arguments
    private static Properties parseArgs(String args){
        Properties props = new Properties();
        //if no agruments specified args shall be null
        if(args == null) return props;
        String newArgs = args.replace('=', '&');
        //System.out.println(newArgs);
        StringTokenizer tokens = new StringTokenizer(newArgs, "&");
        while(tokens.hasMoreTokens()){
            String key = tokens.nextToken();
            String value = tokens.nextToken();
            props.setProperty(key, value);
            //System.out.println("key= " + key + " value= " + value);
        }
        return props;
    }
    
    private static Set<String> parseArgument(String arg){
        Set<String> arguments = new HashSet<String>();
        StringTokenizer tokens = new StringTokenizer(arg, ",");
        while(tokens.hasMoreTokens()){
            arguments.add(tokens.nextToken(arg));
        }
        return arguments;
    }
    */
    private static void registerMBean(){
        
        try{
            Thread t = new Thread(){
                @Override
                public void run(){
                    try{
                        Thread.sleep(10000);
                        System.out.println("Going to register TracerConfigurationMBean");
                        ObjectName name = new ObjectName("rejeev.tracer:type=configuration,name=TracerConfiguration");
                        TracerConfiguration mBean = new TracerConfiguration();
                        //StandardMBean mbean = new StandardMBean(bean, TracerConfigurationMBean.class, false);
                        MBeanServer server = ManagementFactory.getPlatformMBeanServer();
                        server.registerMBean(mBean, name);
                        System.out.println("TracerConfigurationMBean successfully registered");
                    }catch(Exception e){
                        System.out.println("Could not register MBean");
                        e.printStackTrace();
                    }
                }
            };
            t.start();
        }catch(Exception e){
            System.out.println("Could not register MBean");
            e.printStackTrace();
        }
    }
}
